package pr.iceworld.fernando.api.oms.gateway.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pr.iceworld.fernando.oms.service.IUserService;
import pr.iceworld.fernando.oms.util.SessionUtils;
import pr.iceworld.fernando.oms.web.vo.LoginUserVO;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME;

@Controller
@Slf4j
public class LoginController {

    @Autowired
    IUserService iUserService;

    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String login(HttpServletRequest request) {
        log.info("session locale " + request.getSession().getAttribute(LOCALE_SESSION_ATTRIBUTE_NAME));
        return "login";
    }

    @RequestMapping(value = "/loginUser", method = RequestMethod.POST)
    public String login(HttpServletRequest request, Model model, LoginUserVO loginUserVO) throws Exception {
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(loginUserVO.getUsername(), loginUserVO.getPassword(), loginUserVO.getRememberMe());
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);   //完成登录
            model.addAttribute("user", SessionUtils.getCurrentUser());
            return "index";
        } catch(Exception e) {
            Object exception = request.getAttribute("shiroLoginFailure");
            log.info("session locale " + request.getSession().getAttribute(LOCALE_SESSION_ATTRIBUTE_NAME));
            String msg = "";
            if (exception != null){
                if (UnknownAccountException.class.isInstance(exception)) {
                    msg = "提示->账号不存在";
                } else if (IncorrectCredentialsException.class.isInstance(exception)) {
                    msg = "提示->密码不正确";
                } else {
                    msg = "提示->未知错误";
                }
                model.addAttribute("msg", msg);
            }
            return "login";//返回登录页面
        }

    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("user", SessionUtils.getCurrentUser());
        return "index";
    }


    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
        log.info("------------------------ logout start");
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            subject.logout();
        }
        log.info("------------------------ logout end");
        return "login";
    }


}
